import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:jinke/page/pro_list/subs/pro_detail/model/pro_detail_model/pro_detail_model.dart';
import 'package:jinke/page/pro_list/subs/pro_detail/model/pro_enterprise_model.dart';
import 'package:jinke/page/pro_list/subs/pro_detail/model/pro_phase_model/pro_phase_model.dart';
import 'package:jinke/utils/httputils/api.dart';
import 'package:jinke/utils/httputils/resultdata.dart';

import 'index.dart';

class ProDetailController extends GetxController
    with GetSingleTickerProviderStateMixin {
  ProDetailController();

  final state = ProDetailState();

  // tap
  void handleTap(int index) {
    Get.snackbar(
      "标题",
      "消息",
    );
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    state.mod = Get.arguments;
    state.tabTitles = ['基本信息', '项目进展', '每日进展', '五清单', '领导批示', '存在问题'];
    state.tabCtrl = TabController(
        length: state.tabTitles.length, vsync: this, initialIndex: 1);
    state.tabCtrl.addListener(() {
      setupData();
    });
    getProDetail();
  }

  void setupData() {
    var index = state.tabCtrl.index;
    if (index == 0) {
      if (state.phaseList == null) {
        getProDetail();
      }
    } else if (index == 1) {
      if (state.detailMod == null) {
        getProDetail();
      }
    } else if (index == 2) {
      if (state.enterpriseMod == null) {
        getProDetail();
      }
    }
  }

  void getProDetail() {
    ProListApi.detail(id: state.mod.id ?? '').then((value) {
      ResultData res = value;
      if (res.isSuccess) {
        List phases = res.data['saveOrEditPhases'];
        state.phaseList = phases.map((e) => ProPhaseModel.fromJson(e)).toList();
        state.detailMod =
            ProDetailModel.fromJson(res.data['saveOrEditProject']);
        state.enterpriseMod =
            ProEnterpriseModel.fromJson(res.data['saveOrEditEnterprise']);
        update();
      }
    });
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
